{
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Stochastic Gradient Descent Classification"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "import pandas as pd\n",
        "\n",
        "import warnings\n",
        "warnings.filterwarnings(\"ignore\")\n",
        "\n",
        "# yahoo finance is used to fetch data \n",
        "import yfinance as yf\n",
        "yf.pdr_override()"
      ],
      "outputs": [],
      "execution_count": 1,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-09-14T01:07:54.720Z",
          "iopub.execute_input": "2021-09-14T01:07:54.726Z",
          "iopub.status.idle": "2021-09-14T01:07:55.053Z",
          "shell.execute_reply": "2021-09-14T01:07:55.072Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# input\n",
        "symbol = 'AMD'\n",
        "start = '2014-01-01'\n",
        "end = '2018-08-27'\n",
        "\n",
        "# Read data \n",
        "dataset = yf.download(symbol,start,end)\n",
        "\n",
        "# View Columns\n",
        "dataset.head()"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[*********************100%***********************]  1 of 1 completed\n"
          ]
        },
        {
          "output_type": "execute_result",
          "execution_count": 2,
          "data": {
            "text/plain": "            Open  High   Low  Close  Adj Close    Volume\nDate                                                    \n2014-01-02  3.85  3.98  3.84   3.95       3.95  20548400\n2014-01-03  3.98  4.00  3.88   4.00       4.00  22887200\n2014-01-06  4.01  4.18  3.99   4.13       4.13  42398300\n2014-01-07  4.19  4.25  4.11   4.18       4.18  42932100\n2014-01-08  4.23  4.26  4.14   4.18       4.18  30678700",
            "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Open</th>\n      <th>High</th>\n      <th>Low</th>\n      <th>Close</th>\n      <th>Adj Close</th>\n      <th>Volume</th>\n    </tr>\n    <tr>\n      <th>Date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2014-01-02</th>\n      <td>3.85</td>\n      <td>3.98</td>\n      <td>3.84</td>\n      <td>3.95</td>\n      <td>3.95</td>\n      <td>20548400</td>\n    </tr>\n    <tr>\n      <th>2014-01-03</th>\n      <td>3.98</td>\n      <td>4.00</td>\n      <td>3.88</td>\n      <td>4.00</td>\n      <td>4.00</td>\n      <td>22887200</td>\n    </tr>\n    <tr>\n      <th>2014-01-06</th>\n      <td>4.01</td>\n      <td>4.18</td>\n      <td>3.99</td>\n      <td>4.13</td>\n      <td>4.13</td>\n      <td>42398300</td>\n    </tr>\n    <tr>\n      <th>2014-01-07</th>\n      <td>4.19</td>\n      <td>4.25</td>\n      <td>4.11</td>\n      <td>4.18</td>\n      <td>4.18</td>\n      <td>42932100</td>\n    </tr>\n    <tr>\n      <th>2014-01-08</th>\n      <td>4.23</td>\n      <td>4.26</td>\n      <td>4.14</td>\n      <td>4.18</td>\n      <td>4.18</td>\n      <td>30678700</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
          },
          "metadata": {}
        }
      ],
      "execution_count": 2,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-09-14T01:07:55.060Z",
          "iopub.execute_input": "2021-09-14T01:07:55.065Z",
          "iopub.status.idle": "2021-09-14T01:07:55.789Z",
          "shell.execute_reply": "2021-09-14T01:07:55.930Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "dataset = dataset.reset_index()"
      ],
      "outputs": [],
      "execution_count": 3,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-09-14T01:07:55.797Z",
          "iopub.execute_input": "2021-09-14T01:07:55.803Z",
          "iopub.status.idle": "2021-09-14T01:07:55.812Z",
          "shell.execute_reply": "2021-09-14T01:07:55.935Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "dataset['Increase_Decrease'] = np.where(dataset['Volume'].shift(-1) > dataset['Volume'],'Increase','Decrease')\n",
        "dataset['Buy_Sell_on_Open'] = np.where(dataset['Open'].shift(-1) > dataset['Open'],1,0)\n",
        "dataset['Buy_Sell'] = np.where(dataset['Adj Close'].shift(-1) > dataset['Adj Close'],1,0)\n",
        "dataset['Return'] = dataset['Adj Close'].pct_change()\n",
        "dataset = dataset.dropna()\n",
        "dataset.head()"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 4,
          "data": {
            "text/plain": "        Date  Open  High   Low  Close  Adj Close    Volume Increase_Decrease  \\\n1 2014-01-03  3.98  4.00  3.88   4.00       4.00  22887200          Increase   \n2 2014-01-06  4.01  4.18  3.99   4.13       4.13  42398300          Increase   \n3 2014-01-07  4.19  4.25  4.11   4.18       4.18  42932100          Decrease   \n4 2014-01-08  4.23  4.26  4.14   4.18       4.18  30678700          Decrease   \n5 2014-01-09  4.20  4.23  4.05   4.09       4.09  30667600          Decrease   \n\n   Buy_Sell_on_Open  Buy_Sell    Return  \n1                 1         1  0.012658  \n2                 1         1  0.032500  \n3                 1         0  0.012106  \n4                 0         0  0.000000  \n5                 0         1 -0.021531  ",
            "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Date</th>\n      <th>Open</th>\n      <th>High</th>\n      <th>Low</th>\n      <th>Close</th>\n      <th>Adj Close</th>\n      <th>Volume</th>\n      <th>Increase_Decrease</th>\n      <th>Buy_Sell_on_Open</th>\n      <th>Buy_Sell</th>\n      <th>Return</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>2014-01-03</td>\n      <td>3.98</td>\n      <td>4.00</td>\n      <td>3.88</td>\n      <td>4.00</td>\n      <td>4.00</td>\n      <td>22887200</td>\n      <td>Increase</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0.012658</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2014-01-06</td>\n      <td>4.01</td>\n      <td>4.18</td>\n      <td>3.99</td>\n      <td>4.13</td>\n      <td>4.13</td>\n      <td>42398300</td>\n      <td>Increase</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0.032500</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>2014-01-07</td>\n      <td>4.19</td>\n      <td>4.25</td>\n      <td>4.11</td>\n      <td>4.18</td>\n      <td>4.18</td>\n      <td>42932100</td>\n      <td>Decrease</td>\n      <td>1</td>\n      <td>0</td>\n      <td>0.012106</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>2014-01-08</td>\n      <td>4.23</td>\n      <td>4.26</td>\n      <td>4.14</td>\n      <td>4.18</td>\n      <td>4.18</td>\n      <td>30678700</td>\n      <td>Decrease</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.000000</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>2014-01-09</td>\n      <td>4.20</td>\n      <td>4.23</td>\n      <td>4.05</td>\n      <td>4.09</td>\n      <td>4.09</td>\n      <td>30667600</td>\n      <td>Decrease</td>\n      <td>0</td>\n      <td>1</td>\n      <td>-0.021531</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
          },
          "metadata": {}
        }
      ],
      "execution_count": 4,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-09-14T01:07:55.819Z",
          "iopub.execute_input": "2021-09-14T01:07:55.823Z",
          "shell.execute_reply": "2021-09-14T01:07:55.939Z",
          "iopub.status.idle": "2021-09-14T01:07:55.837Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "dataset.tail()"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 5,
          "data": {
            "text/plain": "           Date       Open   High        Low      Close  Adj Close     Volume  \\\n1166 2018-08-20  19.790001  20.08  19.350000  19.980000  19.980000   62983200   \n1167 2018-08-21  19.980000  20.42  19.860001  20.400000  20.400000   55629000   \n1168 2018-08-22  20.280001  20.92  20.209999  20.900000  20.900000   62002700   \n1169 2018-08-23  21.190001  22.32  21.139999  22.290001  22.290001  113444100   \n1170 2018-08-24  22.910000  24.00  22.670000  23.980000  23.980000  164328200   \n\n     Increase_Decrease  Buy_Sell_on_Open  Buy_Sell    Return  \n1166          Decrease                 1         1  0.010622  \n1167          Increase                 1         1  0.021021  \n1168          Increase                 1         1  0.024510  \n1169          Increase                 1         1  0.066507  \n1170          Decrease                 0         0  0.075819  ",
            "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Date</th>\n      <th>Open</th>\n      <th>High</th>\n      <th>Low</th>\n      <th>Close</th>\n      <th>Adj Close</th>\n      <th>Volume</th>\n      <th>Increase_Decrease</th>\n      <th>Buy_Sell_on_Open</th>\n      <th>Buy_Sell</th>\n      <th>Return</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1166</th>\n      <td>2018-08-20</td>\n      <td>19.790001</td>\n      <td>20.08</td>\n      <td>19.350000</td>\n      <td>19.980000</td>\n      <td>19.980000</td>\n      <td>62983200</td>\n      <td>Decrease</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0.010622</td>\n    </tr>\n    <tr>\n      <th>1167</th>\n      <td>2018-08-21</td>\n      <td>19.980000</td>\n      <td>20.42</td>\n      <td>19.860001</td>\n      <td>20.400000</td>\n      <td>20.400000</td>\n      <td>55629000</td>\n      <td>Increase</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0.021021</td>\n    </tr>\n    <tr>\n      <th>1168</th>\n      <td>2018-08-22</td>\n      <td>20.280001</td>\n      <td>20.92</td>\n      <td>20.209999</td>\n      <td>20.900000</td>\n      <td>20.900000</td>\n      <td>62002700</td>\n      <td>Increase</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0.024510</td>\n    </tr>\n    <tr>\n      <th>1169</th>\n      <td>2018-08-23</td>\n      <td>21.190001</td>\n      <td>22.32</td>\n      <td>21.139999</td>\n      <td>22.290001</td>\n      <td>22.290001</td>\n      <td>113444100</td>\n      <td>Increase</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0.066507</td>\n    </tr>\n    <tr>\n      <th>1170</th>\n      <td>2018-08-24</td>\n      <td>22.910000</td>\n      <td>24.00</td>\n      <td>22.670000</td>\n      <td>23.980000</td>\n      <td>23.980000</td>\n      <td>164328200</td>\n      <td>Decrease</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.075819</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
          },
          "metadata": {}
        }
      ],
      "execution_count": 5,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-09-14T01:07:55.845Z",
          "iopub.execute_input": "2021-09-14T01:07:55.852Z",
          "iopub.status.idle": "2021-09-14T01:07:55.868Z",
          "shell.execute_reply": "2021-09-14T01:07:55.942Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "dataset.shape"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 6,
          "data": {
            "text/plain": "(1170, 11)"
          },
          "metadata": {}
        }
      ],
      "execution_count": 6,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-09-14T01:07:55.878Z",
          "iopub.execute_input": "2021-09-14T01:07:55.883Z",
          "iopub.status.idle": "2021-09-14T01:07:55.894Z",
          "shell.execute_reply": "2021-09-14T01:07:55.945Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.linear_model import SGDClassifier"
      ],
      "outputs": [],
      "execution_count": 7,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-09-14T01:07:55.903Z",
          "iopub.execute_input": "2021-09-14T01:07:55.908Z",
          "iopub.status.idle": "2021-09-14T01:07:56.128Z",
          "shell.execute_reply": "2021-09-14T01:07:56.358Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "X = np.array(dataset['Adj Close']).reshape(1170,-1)\n",
        "Y = np.array(dataset['Buy_Sell']).reshape(1170,-1)"
      ],
      "outputs": [],
      "execution_count": 8,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-09-14T01:07:56.137Z",
          "iopub.execute_input": "2021-09-14T01:07:56.142Z",
          "iopub.status.idle": "2021-09-14T01:07:56.150Z",
          "shell.execute_reply": "2021-09-14T01:07:56.362Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "sgd = SGDClassifier(loss=\"hinge\", penalty=\"l2\", max_iter=5)\n",
        "sgd.fit(X, Y)"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 9,
          "data": {
            "text/plain": "SGDClassifier(alpha=0.0001, average=False, class_weight=None, epsilon=0.1,\n       eta0=0.0, fit_intercept=True, l1_ratio=0.15,\n       learning_rate='optimal', loss='hinge', max_iter=5, n_iter=None,\n       n_jobs=1, penalty='l2', power_t=0.5, random_state=None,\n       shuffle=True, tol=None, verbose=0, warm_start=False)"
          },
          "metadata": {}
        }
      ],
      "execution_count": 9,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-09-14T01:07:56.158Z",
          "iopub.execute_input": "2021-09-14T01:07:56.162Z",
          "iopub.status.idle": "2021-09-14T01:07:56.173Z",
          "shell.execute_reply": "2021-09-14T01:07:56.367Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "sgd.coef_"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 10,
          "data": {
            "text/plain": "array([[-6.05928235]])"
          },
          "metadata": {}
        }
      ],
      "execution_count": 10,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-09-14T01:07:56.180Z",
          "iopub.execute_input": "2021-09-14T01:07:56.187Z",
          "iopub.status.idle": "2021-09-14T01:07:56.197Z",
          "shell.execute_reply": "2021-09-14T01:07:56.370Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "sgd.intercept_ "
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 11,
          "data": {
            "text/plain": "array([-6.65650695])"
          },
          "metadata": {}
        }
      ],
      "execution_count": 11,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-09-14T01:07:56.204Z",
          "iopub.execute_input": "2021-09-14T01:07:56.208Z",
          "iopub.status.idle": "2021-09-14T01:07:56.222Z",
          "shell.execute_reply": "2021-09-14T01:07:56.373Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "sgd.predict(Y)"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 12,
          "data": {
            "text/plain": "array([0, 0, 0, ..., 0, 0, 0])"
          },
          "metadata": {}
        }
      ],
      "execution_count": 12,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-09-14T01:07:56.231Z",
          "iopub.execute_input": "2021-09-14T01:07:56.236Z",
          "iopub.status.idle": "2021-09-14T01:07:56.249Z",
          "shell.execute_reply": "2021-09-14T01:07:56.376Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "clf = SGDClassifier(loss=\"log\", max_iter=5).fit(X, Y)\n",
        "clf.predict_proba(Y)"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 13,
          "data": {
            "text/plain": "array([[9.97605461e-01, 2.39453929e-03],\n       [9.97605461e-01, 2.39453929e-03],\n       [9.99859984e-01, 1.40015727e-04],\n       ...,\n       [9.97605461e-01, 2.39453929e-03],\n       [9.97605461e-01, 2.39453929e-03],\n       [9.99859984e-01, 1.40015727e-04]])"
          },
          "metadata": {}
        }
      ],
      "execution_count": 13,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-09-14T01:07:56.257Z",
          "iopub.execute_input": "2021-09-14T01:07:56.261Z",
          "iopub.status.idle": "2021-09-14T01:07:56.271Z",
          "shell.execute_reply": "2021-09-14T01:07:56.378Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn import model_selection # Binary Number\n",
        "\n",
        "kfold = model_selection.KFold(n_splits=10, random_state=7, shuffle=True)\n",
        "results = model_selection.cross_val_score(clf, X, Y, cv=kfold, scoring='accuracy')\n",
        "print(\"Accuracy: %.3f (%.3f)\" % (results.mean(), results.std()))"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Accuracy: 0.491 (0.026)\n"
          ]
        }
      ],
      "execution_count": 14,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-09-14T01:07:56.277Z",
          "iopub.execute_input": "2021-09-14T01:07:56.282Z",
          "iopub.status.idle": "2021-09-14T01:07:56.292Z",
          "shell.execute_reply": "2021-09-14T01:07:56.381Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "results = model_selection.cross_val_score(clf, X, Y, cv=kfold, scoring='neg_log_loss')\n",
        "print(\"Logloss: %.3f (%.3f)\" % (results.mean(), results.std()))"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Logloss: -12.688 (3.612)\n"
          ]
        }
      ],
      "execution_count": 15,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-09-14T01:07:56.298Z",
          "iopub.execute_input": "2021-09-14T01:07:56.304Z",
          "iopub.status.idle": "2021-09-14T01:07:56.314Z",
          "shell.execute_reply": "2021-09-14T01:07:56.383Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "results = model_selection.cross_val_score(clf, X, Y, cv=kfold, scoring='roc_auc')\n",
        "print(\"AUC: %.3f (%.3f)\" % (results.mean(), results.std()))"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "AUC: 0.484 (0.083)\n"
          ]
        }
      ],
      "execution_count": 16,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-09-14T01:07:56.321Z",
          "iopub.execute_input": "2021-09-14T01:07:56.325Z",
          "iopub.status.idle": "2021-09-14T01:07:56.335Z",
          "shell.execute_reply": "2021-09-14T01:07:56.385Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "results = model_selection.cross_val_score(clf, X, Y, cv=kfold, scoring='neg_mean_absolute_error')\n",
        "print(\"MAE: %.3f (%.3f)\" % (results.mean(), results.std()))"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "MAE: -0.513 (0.019)\n"
          ]
        }
      ],
      "execution_count": 19,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-09-14T01:08:12.797Z",
          "iopub.execute_input": "2021-09-14T01:08:12.802Z",
          "shell.execute_reply": "2021-09-14T01:08:12.824Z",
          "iopub.status.idle": "2021-09-14T01:08:12.814Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "results = model_selection.cross_val_score(clf, X, Y, cv=kfold, scoring='neg_mean_squared_error')\n",
        "print(\"MSE: %.3f (%.3f)\" % (results.mean(), results.std()))"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "MSE: -0.503 (0.022)\n"
          ]
        }
      ],
      "execution_count": 20,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-09-14T01:08:52.612Z",
          "iopub.execute_input": "2021-09-14T01:08:52.620Z",
          "iopub.status.idle": "2021-09-14T01:08:52.640Z",
          "shell.execute_reply": "2021-09-14T01:08:52.646Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "results = model_selection.cross_val_score(clf, X, Y, cv=kfold, scoring='r2')\n",
        "print(\"R^2: %.3f (%.3f)\" % (results.mean(), results.std()))"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "R^2: -0.967 (0.096)\n"
          ]
        }
      ],
      "execution_count": 21,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-09-14T01:09:35.819Z",
          "iopub.execute_input": "2021-09-14T01:09:35.827Z",
          "iopub.status.idle": "2021-09-14T01:09:35.841Z",
          "shell.execute_reply": "2021-09-14T01:09:35.847Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.metrics import r2_score # Continous and Binary\n",
        "r2_score(X,Y)"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 22,
          "data": {
            "text/plain": "-1.7917824267171234"
          },
          "metadata": {}
        }
      ],
      "execution_count": 22,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2021-09-14T01:09:37.970Z",
          "iopub.execute_input": "2021-09-14T01:09:37.975Z",
          "iopub.status.idle": "2021-09-14T01:09:37.987Z",
          "shell.execute_reply": "2021-09-14T01:09:37.992Z"
        }
      }
    }
  ],
  "metadata": {
    "kernel_info": {
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.6.13",
      "mimetype": "text/x-python",
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "pygments_lexer": "ipython3",
      "nbconvert_exporter": "python",
      "file_extension": ".py"
    },
    "kernelspec": {
      "name": "python3",
      "language": "python",
      "display_name": "Python 3"
    },
    "nteract": {
      "version": "0.28.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}